package com.huangfuyi.datamodel.dao;

import cn.hutool.core.bean.BeanUtil;
import com.huangfuyi.datamodel.entity.EDataModelInitConnectInfo;
import com.huangfuyi.datamodel.exceptions.EDataModelInitConnInfoQueryException;
import com.huangfuyi.datamodel.exceptions.EDataModelInitConnInfoSaveException;
import com.huangfuyi.datasource.entity.EConnectConf;
import com.huangfuyi.datasource.entity.EDataDefineConf;
import com.huangfuyi.datasource.entity.EDataQueryCondition;
import com.huangfuyi.datasource.entity.ddl.EFileDataDefineConf;
import com.huangfuyi.datasource.entity.query.EDataQueryResult;
import com.huangfuyi.datasource.enums.EDataSourceType;
import com.huangfuyi.datasource.service.EDataSource;
import com.huangfuyi.datasource.factory.EDataSourceFactory;
import com.huangfuyi.datasource.service.EFileDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 数据模型持久层
 */
@Component
public class EDataModelInitDao {

    // 数据模型的数据源信息id
    public static final String DATA_MODEL_SOURCE_ID = "d21ab9d1-7e36-4981-8bad-4225dba68d27";

    // 持久层链接信息
    private EDataModelInitConnectInfo connectInfo;

    // 数据模型持久层链接
    private EDataSource dataSource;

    public int saveDataModelConnectInfo(EDataSourceType dataSourceType, List<EConnectConf> connectInfo) {
        EFileDataSource fileSysDataSource = (EFileDataSource) EDataSourceFactory.getDataSource(EDataSourceType.FILE_SYS, Collections.emptyList());

        EFileDataDefineConf fileDataDefine = new EFileDataDefineConf();
        fileDataDefine.setDatabase("system");
        fileDataDefine.setTable("connect_info");
        fileDataDefine.setId(DATA_MODEL_SOURCE_ID);

        int insert = fileSysDataSource.insert(fileDataDefine, Collections.singletonList(new EDataModelInitConnectInfo(dataSourceType, connectInfo)));
        if (insert > 0) {
            this.connectInfo = new EDataModelInitConnectInfo(dataSourceType, connectInfo);
            return insert;
        } else {
            throw new EDataModelInitConnInfoSaveException("保存数据模型模块数据源链接信息失败！");
        }
    }

    public EDataModelInitConnectInfo queryDataModelConnectInfo() {
        EFileDataSource fileSysDataSource = (EFileDataSource) EDataSourceFactory.getDataSource(EDataSourceType.FILE_SYS, Collections.emptyList());

        EFileDataDefineConf fileDataDefine = new EFileDataDefineConf();
        fileDataDefine.setDatabase("system");
        fileDataDefine.setTable("connect_info");
        fileDataDefine.setId(DATA_MODEL_SOURCE_ID);

        List<Object> connectInfos = fileSysDataSource.select(fileDataDefine, new EDataQueryCondition());
        if (ObjectUtils.isEmpty(connectInfos)) {
            throw new EDataModelInitConnInfoQueryException("没有查询到数据模型模块数据源链接信息。");
        }
        if (!(connectInfos.get(0) instanceof EDataModelInitConnectInfo)) {
            throw new EDataModelInitConnInfoQueryException("已保存的数据模型模块数据源链接信息不正确。");
        }
        EDataModelInitConnectInfo initConnectInfo = (EDataModelInitConnectInfo) connectInfos.get(0);
        this.connectInfo = (EDataModelInitConnectInfo) connectInfos.get(0);
        return initConnectInfo;
    }

    public synchronized void initDataSource() {
        if(!ObjectUtils.isEmpty(this.dataSource)){
            return;
        }
        if (ObjectUtils.isEmpty(this.connectInfo)) {
            queryDataModelConnectInfo();
        }
        this.dataSource = EDataSourceFactory.getDataSource(this.connectInfo.getType(), this.connectInfo.getConnectInfos());
    }

    public EDataSource getDataSource() {
        initDataSource();
        return dataSource;
    }
}
